package cn.com.higinet.dl.joa.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.higinet.dl.joa.common.IdUtil;
import cn.com.higinet.dl.joa.dao.SimpleDao;
import cn.com.higinet.dl.joa.service.AdminService;
@Service("adminService")
public class AdminServiceImpl implements AdminService{
	@Autowired
	private SimpleDao joaSimpleDao;

	public SimpleDao getJoaSimpleDao() {
		return joaSimpleDao;
	}

	public void setJoaSimpleDao(SimpleDao joaSimpleDao) {
		this.joaSimpleDao = joaSimpleDao;
	}
	@Override
	public List<Map<String, Object>> getAllUser() {
		// TODO Auto-generated method stub
		String sql = "select * from tp_users ";
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(null, null);
		//params.put("userType", userType);
		return joaSimpleDao.queryForList(sql, params);
	}
	@Override
	public List<Map<String, Object>> getUserList() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		String sql = "select * from tp_users where userType = 2";
		Map<String, Object> map = new HashMap<String, Object>();
		list = joaSimpleDao.queryForList(sql, map);
		return list;
	}

	@Override
	public String addUser(Map<String, Object> reqs) {
		// TODO Auto-generated method stub
		String result = "success";
		String userId = IdUtil.uuid();
		reqs.put("userId", userId);
		reqs.put("userType", 2);
		try {
			String sql="insert into tp_users (userId, userName, userPwd, userType,realName)"
					+ " values(:userId,:userName,:userPwd,:userType,:realName)";
			joaSimpleDao.executeUpdate(sql, reqs);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = "failed";
		}
		return result;
	}

	@Override
	public String deleteUser(Map<String, Object> reqs) {
		// TODO Auto-generated method stub
		String result = "success";
		String sql = "delete from tp_users where userId=:userId";
		String sql2 = "delete from tp_focus where  userId1=:userId";
		String sql3 = "delete from tp_forum_manage where  userId=:userId";
		String sql4 = "delete from tp_post where  userId=:userId";
		String sql5 = "delete from tp_topic  where  userId=:userId";
		try {
			joaSimpleDao.executeUpdate(sql, reqs);
			joaSimpleDao.executeUpdate(sql2, reqs);
			joaSimpleDao.executeUpdate(sql3, reqs);
			joaSimpleDao.executeUpdate(sql4, reqs);
			joaSimpleDao.executeUpdate(sql5, reqs);
		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = "failed";
		}
		return result;
	}

	@Override
	public String releaseNotice(Map<String, Object> reqs) {
		// TODO Auto-generated method stub
		String result = "success";
		String noticeId = IdUtil.uuid();
		reqs.put("noticeId", noticeId);
		
		try {
			String sql="insert into tp_notice (userId, noticeId, noticeTitle, noticeText)"
					+ " values(:userId,:noticeId,:noticeTitle,:noticeText)";
			joaSimpleDao.executeUpdate(sql, reqs);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = "failed";
		}
		return result;
	}

	@Override
	public String setDigest(Map<String, Object> conds) {
		// TODO Auto-generated method stub
		String result = "success";

		String sql = "update tp_topic set digest = 2  where topicId = :topicId";
		try{
			joaSimpleDao.executeUpdate(sql, conds);
			
			
		}catch(Exception e){
			e.printStackTrace();
			result = "failed";
		}
	return result;
	}

	
}
